package cn.tedu.system.merchant.controller;

import cn.tedu.system.base.response.JsonResult;
import cn.tedu.system.merchant.pojo.dto.*;
import cn.tedu.system.merchant.pojo.vo.MerchantVO;
import cn.tedu.system.merchant.service.MerchantService;
import cn.tedu.system.product.pojo.dto.ProductDto;
import cn.tedu.system.product.pojo.dto.ProductPutDto;
import cn.tedu.system.product.pojo.dto.ProductReviseDto;
import cn.tedu.system.product.pojo.vo.ProductVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.List;

@Slf4j
@Api(tags = "02.商家模块")
@RestController
@RequestMapping("/api/v1/merchant/")
@CrossOrigin(origins = "http://localhost:9090", allowCredentials = "true")  // 允许携带凭证
public class MerchantController {
    @Autowired
    private MerchantService merchantService;

    @ApiOperation("添加店铺功能")
    @ApiOperationSupport(order = 10)
    @PostMapping("addStore")
    public JsonResult addStore(@RequestBody MerchantDto merchantDto, @ApiIgnore HttpSession session) {
        log.debug("添加店铺参数:{}", merchantDto);
        merchantService.addStore(merchantDto,session);
        return JsonResult.ok();
    }

    @ApiOperation("删除店铺功能")
    @ApiOperationSupport(order = 20)
    @PostMapping("delStore")
    public JsonResult delStore(@RequestBody StoreDeleteDTO storeDeleteDTO, @ApiIgnore HttpSession session) {
        log.debug("解析后的参数: {}", storeDeleteDTO);
        merchantService.delStore(storeDeleteDTO, session);
        return JsonResult.ok();
    }
    @ApiOperation("修改店铺信息功能")
    @ApiOperationSupport(order = 30)
    @PostMapping("reviseStore")
    public JsonResult reviseStore(@RequestBody MerchantReviseDto merchantReviseDto, @ApiIgnore HttpSession session) {
        log.debug("修改店铺参数:{}",merchantReviseDto);
        merchantService.reviseStore(merchantReviseDto,session);
        return JsonResult.ok();
    }

    @ApiOperation("查看店铺功能")
    @ApiOperationSupport(order = 40)
    @GetMapping("selStore")
    public JsonResult selStore(@ApiIgnore HttpSession session) {
        List<MerchantVO> list = merchantService.selStore(session);
        return JsonResult.ok(list);
    }

    @ApiOperation("添加商品功能")
    @ApiOperationSupport(order = 50)
    @PostMapping("addProduct")
    public JsonResult addProduct(@RequestBody ProductDto productDto, @ApiIgnore HttpSession session) {
        log.debug("添加商品参数:{}",productDto);
        merchantService.addProduct(productDto,session);
        return JsonResult.ok();
    }

    @ApiOperation("删除商品功能")
    @ApiOperationSupport(order = 60)
    @PostMapping("delProduct")
    public JsonResult delProduct(@RequestBody ProductDeleteDTO deleteDTO, // 使用DTO封装参数
                                 @ApiIgnore HttpSession session) {
        log.debug("删除商品参数: {}", deleteDTO);
        merchantService.delProduct(deleteDTO, session); // 传递DTO对象
        return JsonResult.ok();
    }

    @ApiOperation("修改商品信息功能")
    @ApiOperationSupport(order = 70)
    @PostMapping("reviseProduct")
    public JsonResult reviseProduct(@RequestBody ProductReviseDto productDto, @ApiIgnore HttpSession session) {
        log.debug("修改商品参数:{}",productDto);
        merchantService.reviseProduct(productDto,session);
        return JsonResult.ok();
    }

    @ApiOperation("商家查看所有商品功能")
    @ApiOperationSupport(order = 80)
    @GetMapping("/selProduct")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "merchantId", value = "商家ID", required = true, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "id", value = "商品ID", required = false, dataType = "Long", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "商品状态", required = false, dataType = "Integer", paramType = "query")
    })
    public JsonResult selAllProduct(@RequestParam Long merchantId, @RequestParam(required = false) Long id, @RequestParam(required = false) Integer status,@ApiIgnore HttpSession session) {
        ProductPutDto productPutDto = new ProductPutDto();
        productPutDto.setMerchantId(merchantId);
        productPutDto.setId(id);
        productPutDto.setStatus(status);
        List<ProductVO> list = merchantService.selAllProduct(productPutDto,  session);
        return JsonResult.ok(list);
    }

    @ApiOperation("商品上架下架功能")
    @ApiOperationSupport(order = 90)
    @PostMapping("isPutProduct")
    public JsonResult isPutProduct(@RequestBody ProductPutDto productPutDto, @ApiIgnore HttpSession session) {
        log.debug("商品上下架参数:{}",productPutDto);
        merchantService.isPutProduct(productPutDto,session);
        return JsonResult.ok();
    }
}
